package com.gitee.wsl.ext.reflect

import kotlin.reflect.KClass
import kotlin.reflect.KType

fun KClass<*>.extractQualifiedNameOrThrow() = simpleName ?: throw IllegalArgumentException("can't extract type qualified name, is it local, nested or anonymous? use direct key overload")

/**
 * @return a user friendly string
 */
fun KClass<*>.userFriendly(): String {
    // Add spaces between capital letters.
    return this.simpleName?.replace(Regex("(\\p{Ll})(\\p{Lu})"), "$1 $2").toString()
}

@PublishedApi
internal fun <T : Number> KClass<T>.zero(): T = when (this) {
    Int::class -> 0 as T
    Byte::class -> 0.toByte() as T
    Short::class -> 0.toShort() as T
    Long::class -> 0.toLong() as T
    Double::class -> 0.toDouble() as T
    Float::class -> 0.toFloat() as T
    //BigDecimal::class -> BigDecimal.ZERO as T
    //BigInteger::class -> BigInteger.ZERO as T
    Number::class -> 0 as T
    else -> TODO()
}

expect val KClass<*>.qualifiedNameForComparison: String?


fun KType.canonicalize(): KType {
    return when (this) {
        is KTypeImpl -> this
        else ->
            KTypeImpl(
                classifier = classifier,
                arguments = arguments.map { it.copy(type = it.type?.canonicalize()) },
                isMarkedNullable = isMarkedNullable,
                annotations = emptyList(),
            )
    }
}